package com.mxfcode.back.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mxfcode.back.mapper.CategoryMapper;
import com.mxfcode.back.service.CategoryService;
import com.mxfcode.bbs_common.date.DateUtils;
import com.mxfcode.bbs_common.response.PageDataResult;
import com.mxfcode.common.HostHolder;
import com.mxfcode.entity.BBSCategory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author moxiufeng
 * @date 2021/3/7 23:25
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    private static final Logger logger = LoggerFactory.getLogger(CategoryServiceImpl.class);

    @Autowired
    private HostHolder hostHolder;
    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public PageDataResult getCategoryList(Integer pageNum, Integer pageSize) {
        PageDataResult pageDataResult = new PageDataResult();
        String userId = hostHolder.getUser().getId();
        List<BBSCategory> categoryList = categoryMapper.getCategoryList(userId);

        PageHelper.startPage(pageNum, pageSize);

        if(categoryList.size() != 0){
            PageInfo<BBSCategory> pageInfo = new PageInfo<>(categoryList);
            pageDataResult.setList(categoryList);
            pageDataResult.setTotals((int) pageInfo.getTotal());
        }
        return pageDataResult;
    }

    @Override
    public Map<String, Object> addCategory(BBSCategory category) {
        Map<String,Object> data = new HashMap();
        try {
            //默认字段
            category.setCreateDate(DateUtils.getCurrentDateToDate());
            category.setUserId(hostHolder.getUser().getId());
            category.setCreateBy(hostHolder.getUser().getUserName());
            category.setCreateName(hostHolder.getUser().getRealName());
            category.setUpdateDate(DateUtils.getCurrentDateToDate());
            category.setUpdateBy(hostHolder.getUser().getUserName());
            category.setUpdateName(hostHolder.getUser().getRealName());
            category.setDeleted(1);

            int result = categoryMapper.insertCategory(category);
            if(result == 0){
                data.put("code",0);
                data.put("msg","新增失败！");
                logger.error("自定义分类标签[新增]，结果=新增失败！");
                return data;
            }
            data.put("code",1);
            data.put("msg","新增成功！");
            logger.info("自定义分类标签[新增]，结果=新增成功！");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("自定义分类标签[新增]异常！", e);
            return data;
        }
        return data;
    }

    @Override
    public Map<String, Object> updateCategory(BBSCategory category) {
        Map<String,Object> data = new HashMap();
        try{
            category.setUpdateName(hostHolder.getUser().getRealName());
            category.setUpdateBy(hostHolder.getUser().getUserName());
            category.setUpdateDate(DateUtils.getCurrentDateToDate());
            int result = categoryMapper.updateCategory(category);
            if(result == 0){
                data.put("code",0);
                data.put("msg","更新失败！");
                logger.error("自定义分类标签[更新]，结果=更新失败！");
                return data;
            }
            data.put("code",1);
            data.put("msg","更新成功！");
            logger.info("自定义分类标签[更新]，结果=更新成功！");
        }catch (Exception e) {
            e.printStackTrace();
            logger.error("自定义分类标签[更新]异常！", e);
            return data;
        }
        return data;
    }

    @Override
    public BBSCategory getCategoryById(Integer id) {
        if(id == null){
            return null;
        }
        return categoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public Map<String, Object> del(Integer id) {
        Map<String, Object> data = new HashMap<>();
        try {
            // 删除权限菜单
            int result = categoryMapper.deleteByPrimaryKey(id);
            if(result == 0){
                data.put("code",0);
                data.put("msg","删除失败");
                logger.error("删除失败");
                return data;
            }
            data.put("code",1);
            data.put("msg","删除成功");
            logger.info("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("删除自定义分类异常！", e);
        }
        return data;
    }

    @Override
    public List<BBSCategory> getAllCategoryList() {
        String userId = hostHolder.getUser().getId();
        List<BBSCategory> categoryList = categoryMapper.getAllCategoryList(userId);
        return categoryList;
    }
}
